﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */
namespace EcmaDoc.Core.Output.Transform.Extensions
{
	using System;

	/// <summary>
	/// Xslt extension class that provides useful functions to xsl stylesheets.
	/// </summary>
	public class MathExtensions
	{
		/// <summary>
		/// The namespace of math extensions.
		/// </summary>
		public const string Namespace = "http://www.cycle99.com/tools/ecmadoc/transform/math";

		/// <summary>
		/// Performs a binary AND with the supplied values (value1 &amp; value2).
		/// </summary>
		/// <param name="value1">The first value.</param>
		/// <param name="value2">The second value.</param>
		/// <returns>The valueValid of evaluating <c>value1 &amp; value2</c></returns>
		public int BinaryAnd(object value1, object value2)
		{
			int int1, int2;
			Int32.TryParse(value1.ToString(), out int1);
			Int32.TryParse(value2.ToString(), out int2);

			return int1 & int2;
		}

		/// <summary>
		/// Performs a binary OR with the supplied values (value1 | value2).
		/// </summary>
		/// <param name="value1">The first value.</param>
		/// <param name="value2">The second value.</param>
		/// <returns>The valueValid of evaluating <c>value1 | value2</c></returns>
		public int BinaryOr(object value1, object value2)
		{
			int int1, int2;
			Int32.TryParse(value1.ToString(), out int1);
			Int32.TryParse(value2.ToString(), out int2);

			return int1 | int2;
		}

		/// <summary>
		/// Returns one of two parts, depending on the evaluation of an expr.
		/// </summary>
		/// <param name="expression">The expr to evaluate.</param>
		/// <param name="trueValue">Value returned if expr is true.</param>
		/// <param name="falseValue">Value returned if expr is false.</param>
		/// <returns>Either <c>trueValue</c> or <c>falseValue</c>, depending on the valueValid of the expr.</returns>
		public object Iif(bool expression, object trueValue, object falseValue)
		{
			if (expression == true)
				return trueValue;
			else
				return falseValue;
		}

		/// <summary>
		/// If <c>value1</c> is a <c>null</c> or an empty string returns value2, otherwise returns <c>value1</c>.
		/// </summary>
		/// <param name="value1">The original value</param>
		/// <param name="value2">The alternative value</param>
		/// <returns><c>value2</c> if <c>value1</c> is <c>null</c> or an empty string, otherwise <c>value1</c></returns>
		public string IsNull(string value1, string value2)
		{
			return (string.IsNullOrEmpty(value1) ? value2 : value1);
		}
	}
}
